package com.example.demo.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.Result;
import com.example.demo.dto.UpdateDto;
import com.example.demo.dto.LoginDto;
import com.example.demo.dto.RegisterDto;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 第五小组
 * @since 2021-08-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;

    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginDto loginDto, HttpServletResponse response){
        User user = userService.getOne(new QueryWrapper<User>().eq("account",loginDto.getAccount()));
        Assert.notNull(user,"用户不存在");
        if(!user.getPassword().equals(loginDto.getPassword())){
            return Result.fail("密码不正确");
        }
        return Result.succ(MapUtil.builder()
                .put("id",user.getId())
                .put("account",user.getAccount())
                .put("username",user.getUsername())
                .put("avatar",user.getAvatar())
                .put("email",user.getEmail())
                .map()
        );
    }

    @PostMapping("/register")
    public Result register(@Validated @RequestBody RegisterDto registerDto, HttpServletResponse response){
        User dbuser = userService.getOne(new QueryWrapper<User>().eq("account",registerDto.getAccount()));
        Assert.isNull(dbuser,"用户已存在");
        User user = new User();
        user.setUsername(registerDto.getUsername());
        user.setAccount(registerDto.getAccount());
        user.setEmail(registerDto.getEmail());
        user.setPassword(registerDto.getPassword());
        user.setCreated(new Date());
        user.setLastLogin(LocalDateTime.now());
        userService.save(user);
        return Result.succ(MapUtil.builder()
                .put("account",user.getAccount())
                .put("username",user.getUsername())
                .put("avatar",user.getAvatar())
                .put("email",user.getEmail())
                .put("status",user.getStatus())
                .put("lastLogin",user.getLastLogin())
                .map()
        );
    }

    @GetMapping("/users")
    public Result users(HttpServletResponse response){
        return Result.succ(userService.listMaps());
    }

    @PostMapping("/query")
    public Result query(@RequestParam(value = "username") String userName, HttpServletResponse response){
        Map<String,Object> dbuser = userService.getMap(new QueryWrapper<User>().eq("username",userName));
        Assert.notNull(dbuser,"查询用户不存在");
        return Result.succ(dbuser);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam(value = "id") int id, HttpServletResponse response){
        Map<String,Object> dbuser = userService.getMap(new QueryWrapper<User>().eq("id",id));
        Assert.notNull(dbuser,"要删除的用户不存在");
        userMapper.deleteById(id);
        return Result.succ(null);
    }

    @PostMapping("/update")
    public Result update(@RequestBody UpdateDto updateDto, HttpServletResponse response){
        User dbuser = userService.getOne(new QueryWrapper<User>().eq("id",updateDto.getId()));
        Assert.notNull(dbuser,"要修改的用户不存在");
        User user = userService.getOne(new QueryWrapper<User>().eq("account",updateDto.getAccount()));
        Assert.isNull(user,"该账号已被使用");
        BeanUtils.copyProperties(updateDto,dbuser);
        userService.updateById(dbuser);
        return Result.succ(dbuser);
    }
}
